package com.kelaite.procurement.service.impl;

import io.jsonwebtoken.lang.Collections;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.dto.CityDTO;
import com.kelaite.procurement.dto.CompanyDTO;
import com.kelaite.procurement.dto.ProvinceDTO;
import com.kelaite.procurement.dto.PurchaseOrganizationDTO;
import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TPurchaseOrganization;
import com.kelaite.procurement.exception.BusinessException;
import com.kelaite.procurement.repository.PurchaseOrganizationRepository;
import com.kelaite.procurement.service.PurchaseOrganizationService;
import com.kelaite.procurement.utils.ConvertUtil;

@Service
@Transactional
public class PurchaseOrganizationServiceImpl implements
		PurchaseOrganizationService {

	@Autowired
	private PurchaseOrganizationRepository purchaseOrganizationRepository;

	@PersistenceContext
	private EntityManager em;

	@Override
	public TPurchaseOrganization save(TPurchaseOrganization persistence) {
		return this.purchaseOrganizationRepository.save(persistence);
	}

	@Override
	public TPurchaseOrganization getObject(Long id) {
		return this.purchaseOrganizationRepository.findOne(id);
	}

	@Override
	public List<TPurchaseOrganization> getAll() {
		return this.purchaseOrganizationRepository.findAll();
	}

	@Override
	public void deleteByObject(TPurchaseOrganization persistence) {
		this.purchaseOrganizationRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.purchaseOrganizationRepository.delete(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TPurchaseOrganization> getByProperty(String property,
			Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TPurchaseOrganization.class)
				.add(Restrictions.eq(property, value));
		return criteria.list();
	}

	@Override
	public List<PurchaseOrganizationDTO> getByCompany(CompanyDTO companyDto) {
		if (null == companyDto || null == companyDto.getId()) {
			throw new BusinessException("company object is null or invalid! ");
		}
		//
		List<TPurchaseOrganization> dbPurchaseOrganizationList = purchaseOrganizationRepository
				.findByTCompany(ConvertUtil.convertObj(companyDto,
						TCompany.class));
		//
		List<PurchaseOrganizationDTO> purchaseOrganizationList = new ArrayList<PurchaseOrganizationDTO>();
		if (!Collections.isEmpty(dbPurchaseOrganizationList)) {
			for (TPurchaseOrganization dbPurchaseOrganization : dbPurchaseOrganizationList) {
				PurchaseOrganizationDTO purchaseOrganizationDTO = ConvertUtil.convertObj(dbPurchaseOrganization, PurchaseOrganizationDTO.class);
				purchaseOrganizationDTO.settCity(ConvertUtil.convertObj(dbPurchaseOrganization.gettCity(), CityDTO.class));
				purchaseOrganizationDTO.settCompany(ConvertUtil.convertObj(dbPurchaseOrganization.gettCompany(), CompanyDTO.class));
				purchaseOrganizationDTO.settProvince(ConvertUtil.convertObj(dbPurchaseOrganization.gettProvince(), ProvinceDTO.class));
				purchaseOrganizationDTO.settUser(ConvertUtil.convertObj(dbPurchaseOrganization.gettUser(), UserDTO.class));
				purchaseOrganizationList.add(purchaseOrganizationDTO);
			}
		}
		return purchaseOrganizationList;
	}

	@Override
	public int getByOrganizationNo(String organizationNo) {

		int count=this.purchaseOrganizationRepository.countByPurchaseOrgAndOrgNo(organizationNo);
		return count;
	}

	
}
